/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ga.coordination;

import java.util.ArrayList;
import java.util.Hashtable;

/**
 *
 * @author hartono
 */
public class Machine {
   /**
     * id mesin, mulai dari 0
     */ 
   public byte idMachine;
   /**
    * beban per hari
    */
   public byte hourPerDay;
   /**
    * beban per periode
    */
   public byte hourPerPeriod;
   /**
    * waktu istirahat
    */
   public byte sleep;
   /**
    * Jumlah mesin yang dihasilkan per jam
    */
   public int productivity;
   
   public int productivityDefault ;
   
   public boolean needRest = false;
   public int restTime = 0;
   public int currentHourInDay = 0;
   public int currentHourPerPeriod = 0;
   
   public void ResetMachine (){       
       productivity = productivityDefault;
       restTime = 0;
       currentHourInDay = 0;
       currentHourPerPeriod = 0;
       totalProduct = 0;
       needRest = false;
   }
   
   /**
    * 
    */
   public byte costOverload;
   /**
    * biaya sewa
    */
   public int rentCost;
   
   /**
    * jumlah bahan baku yang dibutuhkan (I)
    */
   public int totalDependency; 
   /**
    * ID mesin yang dibutuhkan untuk membuat bahan baku indeks mulai dari 0
    */
   public byte idDependency;
   /**
    * harga jual produk yang dihasilkan
    */
   public int profitProduct;
   /**
    * jumlah produk yang dihasilkan oleh mesin ini
    */
   public int totalProduct; 
   
   public ArrayList<Character> idVendor ;

    public Machine() {
        idVendor = new ArrayList<Character>();
    }

    public Machine(byte idMachine, byte hourPerDay, byte hourPerPeriod, byte sleep, int productivity, byte costOverload, int rentCost, int totalDependency, byte idDependency, int profitProduct, int totalProduct, ArrayList<Character> idVendor) {
        this.idMachine = idMachine;
        this.hourPerDay = hourPerDay;
        this.hourPerPeriod = hourPerPeriod;
        this.sleep = sleep;
        this.productivity = productivity;
        this.costOverload = costOverload;
        this.rentCost = rentCost;
        this.totalDependency = totalDependency;
        this.idDependency = idDependency;
        this.profitProduct = profitProduct;
        this.totalProduct = totalProduct;
        this.idVendor = idVendor;
    }
   
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i=0; i< idVendor.size();i++){
            sb.append(idVendor.get(i));
            sb.append(' ');
        }
        
        return "Machine ID:"+idMachine+ " hourPerDay:" + hourPerDay + " hourPerPeriod:" + hourPerPeriod +
                " sleep:" + sleep + " prod:" + productivity + " costOverload:"+costOverload+ " rent:" + rentCost+
                " depend:" + idDependency + " nDepend:" + totalDependency + " profit:" + profitProduct +
                " \ntotalProduct:" + totalProduct + "\n current T "+ currentHourInDay + "\n current W " + currentHourPerPeriod +
                " rest " +restTime + " needRest : " + needRest +
                "\n\tAda di vendor:" + sb
                ;
    }
          
    
    public void ReduceProductivity(){
        productivity -= ((double)costOverload * productivity / 100);
    }
    
    public long TotalProfit(){
        return (long)totalProduct * (long)profitProduct;
    }
    
    public boolean ProducePerHour(){
        if (currentHourInDay < hourPerDay){
            if (currentHourPerPeriod < hourPerPeriod){
                if (needRest){
                    ReduceProductivity();
                }
            }else{             
                // overload
//                if (currentHourPerPeriod ==5){
//                    System.out.println("55555");
//                }
                needRest = true;
                restTime = 0;
                ReduceProductivity();
            }
            currentHourInDay++;
            currentHourPerPeriod++;
            totalProduct += productivity;
            restTime =0;
            
            return true;
        }else{
            return false;
        }
    }
    
    public void Rest(){
        if (currentHourInDay <= hourPerDay ){
            currentHourPerPeriod = 0;
            
            restTime++;
            
            if (restTime >= sleep){               
                
                needRest  =false;
            }
        }
    }
       
    
    /**
     * Mengurangi bahan baku
     * @param n Bahan baku 
     * @return 
     */
    public boolean UseProduct(int n){
        if (totalProduct >= n){
            totalProduct -= n;      
            return true;
        }
        return false;        
    }       
    
    public boolean isDead(){
        return currentHourInDay >= hourPerDay;
    }
}
